package com.xugoqk.decompressioneduction;

import lombok.extern.slf4j.Slf4j;
import utils.*;

import java.io.File;
import java.util.Objects;

/**
 * @author xuguoqing
 * @version 1.0
 * @description: TODO
 * @date 2022/10/22 10:13
 */
@Slf4j
public class Decompression {



    /**
     * 递归解压-解密-压缩
     * @param scanFile 需要遍历的文件夹
     * @throws Exception
     */
    public void recursiveDecompression(File scanFile, String tmpPath) throws Exception {
        if (scanFile.isDirectory()) {
            File [] fileList = scanFile.listFiles();
            for(int i = 0; i < Objects.requireNonNull(fileList).length; i ++){
                File dirFile = fileList[i];
                if(dirFile.isDirectory()){
                    recursiveDecompression(dirFile,tmpPath);
                }else {
                    String fileType = FileUtil.getFileType(dirFile);
                    if (fileType.contains(FileTypeEnum.RAR.getType())) {
                        //rar解压
                        String fileName = FileUtil.getPrefix(dirFile.getName())+ ZipUtils.UNCOMPRESS_TMP+FileUtil.getTimeStr();
                        String uncompressedPath = tmpPath + File.separator+fileName;
                        RarUtil.unRarFile(dirFile.getPath(),uncompressedPath);
                        boolean isDirCompress = FileUtil.isDirCompress(uncompressedPath);
                        //解压后删除压缩包
                        FileUtil.deleteFile(dirFile.getPath());
                        //递归
                        File recursiveFile = new File(uncompressedPath);
                        recursiveDecompression(recursiveFile,tmpPath);
                        //压缩
                        ZipUtils.compressToZip(uncompressedPath,dirFile.getParent(),dirFile.getName(),isDirCompress);
                        FileUtil.deleteFile(uncompressedPath);
                    } else if (fileType.contains(FileTypeEnum.SEVENZ.getType())) {
                        String fileName = FileUtil.getPrefix(dirFile.getName())+ZipUtils.UNCOMPRESS_TMP+FileUtil.getTimeStr();
                        String uncompressedPath = tmpPath +File.separator+fileName;
                        SevenZUtil.sevenZDecompress(dirFile.getPath(),uncompressedPath);
                        boolean isDirCompress = FileUtil.isDirCompress(uncompressedPath);
                        //解压后删除压缩包
                        //FileUtil.deleteFile(dirFile.getPath());
                        //递归
                        File recursiveFile = new File(uncompressedPath);
                        recursiveDecompression(recursiveFile,tmpPath);
                        //压缩
                        ZipUtils.compressToZip(uncompressedPath,dirFile.getParent(),dirFile.getName(),isDirCompress);
                        FileUtil.deleteFile(uncompressedPath);
                    } else if (fileType.contains(FileTypeEnum.ZIP.getType())) {
                        //zip解压
                        String fileName = FileUtil.getPrefix(dirFile.getName())+ ZipUtils.UNCOMPRESS_TMP +FileUtil.getTimeStr();
                        String uncompressedPath = tmpPath +File.separator+fileName;
                        ZipUtils.zipUncompress(dirFile.getPath(),uncompressedPath);
                        boolean isDirCompress = FileUtil.isDirCompress(uncompressedPath);
                        //解压后删除压缩包
                        //FileUtil.deleteFile(dirFile.getPath());
                        //递归
                        File recursiveFile = new File(uncompressedPath);
                        recursiveDecompression(recursiveFile,tmpPath);
                        //zip压缩
                        ZipUtils.compressToZip(uncompressedPath,dirFile.getParent(),dirFile.getName(),isDirCompress);
                        FileUtil.deleteFile(uncompressedPath);
                    }else{
                        //非rar、7z、zip文件当做普通文件处理
                        //普通文件处理逻辑
                        log.info("执行非压缩文件[{}]的处理逻辑",dirFile.getPath());
                    }
                }
            }
        }
    }
}
